package com.framework.wz.wutong.ui.activity;

import android.graphics.Rect;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.framework.wz.wutong.Bean.BaseParams;
import com.framework.wz.wutong.Bean.LoginBean;
import com.framework.wz.wutong.Bean.RegisterParams;
import com.framework.wz.wutong.R;
import com.framework.wz.wutong.base.BaseActivity;
import com.framework.wz.wutong.net.RetrofitHelper;
import com.framework.wz.wutong.rx.scheduler.IoMainScheduler;
import com.framework.wz.wutong.utils.ConstantUtil;
import com.framework.wz.wutong.utils.StatusBarUtil;
import com.framework.wz.wutong.utils.ToastUtil;
import com.google.gson.Gson;
import com.smarx.notchlib.INotchScreen;
import com.smarx.notchlib.NotchScreenManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class RegistActivity extends BaseActivity {

    @BindView(R.id.et_input1)
    EditText etInput1;
    @BindView(R.id.et_input2)
    EditText etInput2;
    @BindView(R.id.et_input3)
    EditText etInput3;
    @BindView(R.id.btn_register)
    Button btnRegister;
    @BindView(R.id.iv_notch)
    ImageView iv_notch;

    //刘海屏适配管理器
    private NotchScreenManager notchScreenManager = NotchScreenManager.getInstance();
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_regist;
    }

    @Override
    protected void initView() {
        //获取刘海屏信息
        notchScreenManager.setDisplayInNotch(this);
        notchScreenManager.getNotchInfo(this, new INotchScreen.NotchScreenCallback() {
            @Override
            public void onResult(INotchScreen.NotchScreenInfo notchScreenInfo) {
                StatusBarUtil.setStatusBarMode(RegistActivity.this, true, R.color.white);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @OnClick({R.id.btn_register, R.id.iv_back})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.btn_register:
                register();
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    private boolean checkParams(){
        if (TextUtils.isEmpty(etInput1.getText().toString())) {
            ToastUtil.show(this, "请输入您的手机号");
            return false;
        }
        if (TextUtils.isEmpty(etInput2.getText().toString())) {
            ToastUtil.show(this, "请设置密码");
            return false;
        }
        if (TextUtils.isEmpty(etInput3.getText().toString())) {
            ToastUtil.show(this, "请再输入一次密码");
            return false;
        }
        if (!TextUtils.equals(etInput2.getText().toString(), etInput3.getText().toString())) {
            ToastUtil.show(this, "两次输入密码不同，请重新输入");
            return false;
        }
        return true;
    }

    private void register(){
        checkParams();
        showDialog(true);
        final String param1 = etInput1.getText().toString().trim();
        final String param2 = etInput2.getText().toString().trim();
        final String param3 = etInput3.getText().toString().trim();

        String str = "";
        BaseParams<RegisterParams> params = new BaseParams<>();
        RegisterParams registerParams = new RegisterParams();
        registerParams.setPhone(param1);
        registerParams.setPassword(param2);
        params.setParameter(registerParams);

        str = new Gson().toJson(params);
        Log.e("123", "str = " + str);

        new RetrofitHelper().getApiService(ConstantUtil.BASE_URL).register(str).compose(new IoMainScheduler<LoginBean>()).subscribe(new Observer<LoginBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(LoginBean bean) {
                showDialog(false);
                if (bean == null) {
                    return;
                }

                if (TextUtils.equals(bean.getStatus(), "200")) {
                    ToastUtil.show(RegistActivity.this, "注册成功");
                } else {
                    ToastUtil.show(RegistActivity.this, bean.getMessage());
                }
            }

            @Override
            public void onError(Throwable e) {
                showDialog(false);
            }

            @Override
            public void onComplete() {

            }
        });
    }
}
